public class Test {// 定义链表节点类
    class Node {
        int data;
        Node next;

        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    // 定义链表类
    class LinkedList {
        private Node head;

        public LinkedList() {
            this.head = null;
        }

        // 在链表末尾添加节点
        public void append(int data) {
            Node newNode = new Node(data);
            if (head == null) {
                head = newNode;
                return;
            }
            Node lastNode = head;
            while (lastNode.next != null) {
                lastNode = lastNode.next;
            }
            lastNode.next = newNode;
        }

        // 删除指定值的节点
        public void delete(int key) {
            if (head == null) {
                return;
            }
            if (head.data == key) {
                head = head.next;
                return;
            }
            Node current = head;
            Node previous = null;
            while (current != null && current.data != key) {
                previous = current;
                current = current.next;
            }
            if (current != null) {
                previous.next = current.next;
            }
        }

        // 打印链表
        public void printList() {
            Node current = head;
            while (current != null) {
                System.out.print(current.data + " ");
                current = current.next;
            }
            System.out.println();
        }
    }

    public class Main {
        public static void main(String[] args) {
            LinkedList list = new LinkedList();
            list.append(1);
            list.append(2);
            list.append(3);
            list.printList();
            list.delete(2);
            list.printList();
        }
    }
}
